package com.cskaoyan.service;

import com.cskaoyan.bean.Coupon;
import com.cskaoyan.bean.CouponExample;
import com.cskaoyan.bean.basebean.BasePageData;
import com.cskaoyan.bean.basebean.BaseParam;
import com.cskaoyan.exception.CouponException;
import com.cskaoyan.mapper.CouponMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author lxy
 * @create 2021-10-18 20:46
 */

@Service
public class CouponServiceImpl implements  CouponService {

    @Autowired
    CouponMapper couponMapper;

    @Override
    public BasePageData query(String name, Short type, Short status, BaseParam baseParam) {

        PageHelper.startPage(baseParam.getPage(),baseParam.getLimit());
        CouponExample example = new CouponExample();
        CouponExample.Criteria criteria = example.createCriteria();

        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }

        if (type != null) {
            criteria.andTypeEqualTo(type);
        }

        if (status != null) {
            criteria.andStatusEqualTo(status);
        }


        example.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());

        List<Coupon> coupons = couponMapper.selectByExample(example);

        return BasePageData.ok(coupons);

    }

    @Override
    public void deleteCoupon(Coupon coupon) throws CouponException {
        coupon.setDeleted(true);
        int i = couponMapper.updateByPrimaryKeySelective(coupon);
        if (i == 0) {
            throw new CouponException();
        }
        return;
    }

    @Override
    public void createCoupon(Coupon coupon) throws CouponException {
        coupon.setAddTime(new Date(System.currentTimeMillis()));
        coupon.setUpdateTime(new Date(System.currentTimeMillis()));
        if (coupon.getType() == 2){
            coupon.setCode(UUID.randomUUID().toString());
        }
        int i = couponMapper.insertSelective(coupon);
        if (i == 0) {
            throw new CouponException();
        }
        return;
    }

    @Override
    public void updateCoupon(Coupon coupon) throws CouponException {

        if (coupon.getType() == 2) {
            if (coupon.getCode() == null) {
                coupon.setCode(UUID.randomUUID().toString());
            }
        } else {
            coupon.setCode(null);
        }
        coupon.setUpdateTime(new Date(System.currentTimeMillis()));
        int i = couponMapper.updateByPrimaryKeySelective(coupon);
        if (i == 0) {
            throw new CouponException();
        }
        return;
    }

    @Override
    public Coupon readCoupon(Integer id) {
        Coupon coupon = couponMapper.selectByPrimaryKey(id);
        return coupon;
    }

    @Override
    public void deleteCouponById(int couponId) {
        couponMapper.deleteByPrimaryKey(couponId);
    }
}
